package com.ck.ck_back.service.impl;

import com.ck.ck_back.dao.LikeProductionMapper;
import com.ck.ck_back.pojo.LikeComment;
import com.ck.ck_back.pojo.LikeProduction;
import com.ck.ck_back.service.likeproductionservice;
import com.ck.ck_back.utils.RedisKeyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class likeproductionserviceImpl implements likeproductionservice {
    @Resource
    private LikeProductionMapper likeProductionMapper;
    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;

    @Override
    public void saveLiked2Redis(int likedProductionId, String likedPostId) {
        String key = RedisKeyUtils.getLikedKey(likedProductionId,likedPostId);
        String value =RedisKeyUtils.getLikedvalue(1,new Date().getTime());
        redisTemplate.opsForHash().put(RedisKeyUtils.MAP_KEY_Production_LIKED,key,value);
    }

    @Override
    public void unlikeFromRedis(int likedProductionId, String likedPostId) {
        String key = RedisKeyUtils.getLikedKey(likedProductionId,likedPostId);
        String value =RedisKeyUtils.getLikedvalue(0,new Date().getTime());
        redisTemplate.opsForHash().put(RedisKeyUtils.MAP_KEY_Production_LIKED,key,value);
    }

    @Override
    public List<LikeProduction> getLikedDataFromRedis() {
        Cursor<Map.Entry<Object, Object>> cursor = redisTemplate.opsForHash().scan(RedisKeyUtils.MAP_KEY_Production_LIKED, ScanOptions.NONE);
        List<LikeProduction> list = new ArrayList<>();
        while (cursor.hasNext()){
            Map.Entry<Object,Object> entry = cursor.next();
            String key = (String) entry.getKey();
            String[] split = key.split("::");
            int likedProductionId = Integer.parseInt(split[0]);
            String likedPostId = split[1];

            String value = (String) entry.getValue();
            String[] splitt = value.split("::");
            Integer state = Integer.parseInt(splitt[0]);
            Long updateTime = Long.valueOf(splitt[1]);

            //组装成 UserLike 对象
            LikeProduction likeProduction = new LikeProduction();
            likeProduction.setLikedProductionId(likedProductionId);
            likeProduction.setLikedPostId(likedPostId);
            likeProduction.setState(state);
            likeProduction.setUpdateTime(updateTime);
            list.add(likeProduction);
            //存到 list 后从 Redis 中删除
            redisTemplate.opsForHash().delete(RedisKeyUtils.MAP_KEY_Production_LIKED, key);
        }
        return list;
    }


    @Override
    public void deal_withdata(List<LikeProduction> likeProductions) {
        //        判断mysql中是否有该评论，有则改，无则加
        for (LikeProduction LikeProduction :likeProductions ) {
            LikeProduction likeproction = likeProductionMapper.selectBytwo_id(LikeProduction.getLikedProductionId(),LikeProduction.getLikedPostId());
            if (likeproction == null){
                likeProductionMapper.insertSelective(LikeProduction);
            }else {
                likeproction.setState(LikeProduction.getState());
                likeproction.setUpdateTime(LikeProduction.getUpdateTime());
                likeProductionMapper.updateByPrimaryKeySelective(likeproction);
            }
        }
    }

    @Override
    public int get_production_state(int likedProductionId, String likedPostId) {
            if(!String.valueOf(likedProductionId).equals("")&&likedPostId!=null){
                String key = RedisKeyUtils.getLikedKey(likedProductionId,likedPostId);
                redisTemplate.opsForHash().get(RedisKeyUtils.MAP_KEY_Production_LIKED,key);
                String obj = (String) redisTemplate.opsForHash().get(RedisKeyUtils.MAP_KEY_Production_LIKED,key);
                if (obj!=null&&!obj.equals("")){
                    String[] split = obj.split("::");
                    Integer state = Integer.parseInt(split[0]);
                    return state;
                }else {
                    LikeProduction likeProduction = likeProductionMapper.selectBytwo_id(likedProductionId,likedPostId);
                    if (likeProduction!=null){
                        return likeProduction.getState();
                    }else {
                        return 0;
                    }
                }
            }else {return 404;}

    }
}
